package top.houry.pointer;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/10/9
 **/
public class TwoPointer {


    public static void main(String[] args) {
//        // 前提是 这个数组默认是有序的
//        int[] array = new int[]{1, 3, 4, 6, 7, 8, 11};
//
//        int[] result = twoSum(array, 15);
//        System.out.println(result[0]);
//        System.out.println(result[1]);


        Date date = new Date();

        Date date1 = new Date(date.getTime() - 1000);
        Date date2 = new Date(date.getTime() + 1000);

        Timestamp beginTime = new Timestamp(date.getTime() - 1000);
        Timestamp endTime = new Timestamp(date.getTime() + 1000);


        System.out.println("====");

    }


    public static List<List<Integer>> threeSum(int[] array) {

        List<List<Integer>> list = new ArrayList<>(1);


        if (null == array || array.length <= 2) {
            return list;
        }

        int length = array.length;
        Arrays.sort(array);

        int[] result = new int[3];
        // 因为双指针，最后一个不计数，所以 -2
        for (int i = 0; i < array.length - 2; i++) {
            // 表示循环的是同一个数，用来去重
            if (i > 0 && array[i - 1] == array[i]) {
                continue;
            }
            int left = i + 1;
            int right = length - 1;

            while (left < right) {
                if (array[i] + array[left] + array[right] == 0) {
                    List<Integer> integerList = new ArrayList<>(3);
                    integerList.add(array[i]);
                    integerList.add(array[left]);
                    integerList.add(array[right]);
                    list.add(integerList);
                    left++;
                    right--;
                    while (left < right && array[left] == array[left - 1]) {
                        left++;
                    }
                    while (left < right && array[right] == array[right + 1]) {
                        right--;
                    }
                } else if (array[i] + array[left] + array[right] > 0) {
                    right--;
                } else {
                    left++;
                }
            }

        }


        return list;
    }


    public static int[] twoSum(int[] array, int target) {
        if (null == array || array.length < 1) {
            return null;
        }
        int[] result = new int[2];
        int start = 0;
        int end = array.length - 1;
        while (start < end) {
            if (array[start] + array[end] < target) {
                start++;
            } else if (array[start] + array[end] > target) {
                end--;
            } else {
                result[0] = start;
                result[1] = end;
                break;
            }

        }
        return result;
    }

}
